package com.yuncommunity.runcircle.conf;

import android.app.Activity;

import com.oldfeel.utils.NetUtil;
import com.oldfeel.utils.StringUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by oldfeel on 15-12-1.
 */
public class Net extends NetUtil {

    public Net(String api) {
        super(api);
    }

    public Net(Activity activity, String api) {
        super(activity, api);
        setParams("access_token", "deaa96f2-27a7-41d7-a7f9-1b7b4c7c0ea7|4131010251");
    }

    @Override
    public boolean isSuccess(String result) {
        return getCode(result) == 0;
    }

    @Override
    public String getData(String result) {
        if (StringUtil.isEmpty(result)) {
            return "";
        }
        try {
            JSONObject json = new JSONObject(result);
            if (json.has("data")) {
                return json.getString("data");
            } else {
                return result;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return super.getData(result);
    }

    @Override
    public int getCode(String result) {
        if (StringUtil.isEmpty(result)) {
            return -1;
        }
        try {
            JSONObject json = new JSONObject(result);
            return json.getInt("result_code");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return super.getCode(result);
    }
}
